package com.util.library.database;

import com.util.library.common.ConfigLib;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper
{
	private static final String TAG = "MySQLiteHelper";

	public static final int DATABASE_VERSION = 1;

	private String _tableName;
	private String _sqlQueryColumn;

	public MySQLiteHelper(Context context, String tableName,
			String sqlQueryColumn, boolean isWriteAccess)
	{
		super(context, ConfigLib.DATABASE_NAME, null, DATABASE_VERSION);
		_tableName = tableName;
		_sqlQueryColumn = sqlQueryColumn;

		/** Open DB */
		if (isWriteAccess)
		{
			getWritableDatabase();
		}
		else
		{
			getReadableDatabase();
		}
	}

	@Override
	public void onCreate(SQLiteDatabase database)
	{
		Log.d(TAG, "Create table if not exists " + _tableName + "("
				+ _sqlQueryColumn + ")");
		
		database.execSQL("CREATE TABLE IF NOT EXISTS " + _tableName + "("
				+ _sqlQueryColumn + ")");

	}

	public void createTable(String tableName, String sqlQueryColumn)
	{
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		Log.d(TAG, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		
		db.execSQL("DROP TABLE IF EXISTS " + _tableName);
		onCreate(db);
	}
}
